/*
 * Copyright (C) 2008-2009 the original author or authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.liquidform.internal;

/**
 * General purpose pair class. One can decide to use <tt>==</tt> on parts in
 * {@link #equals(Object)} implementation, or to use <tt>first.equals()</tt> and
 * <tt>second.equals()</tt> to compare pairs (this is the default).
 * 
 * @param <U>
 *            the type of the first "part"
 * @param <V>
 *            the type of the second "part"
 */
public class Pair<U, V> {

	public final U first;

	public final V second;

	private final boolean useEquals;

	public Pair(U first, V second) {
		this.first = first;
		this.second = second;
		this.useEquals = true;
	}

	public Pair(U first, V second, boolean useEquals) {
		this.first = first;
		this.second = second;
		this.useEquals = useEquals;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((first == null) ? 0 : first.hashCode());
		result = prime * result + ((second == null) ? 0 : second.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Pair<U, V> other = (Pair<U, V>) obj;
		if (first == null) {
			if (other.first != null) {
				return false;
			}
		} else if (useEquals ? !first.equals(other.first)
				: first != other.first) {
			return false;
		}
		if (second == null) {
			if (other.second != null) {
				return false;
			}
		} else if (useEquals ? !second.equals(other.second)
				: second != other.second) {
			return false;
		}
		return true;
	}

}
